package com.itheima.prize.commons.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.prize.commons.config.RedisKeys;
import com.itheima.prize.commons.constant.SystemConstants;
import com.itheima.prize.commons.db.entity.CardGame;
import com.itheima.prize.commons.db.entity.CardGameProduct;
import com.itheima.prize.commons.db.entity.CardGameRules;
import com.itheima.prize.commons.db.entity.CardProduct;
import com.itheima.prize.commons.db.mapper.CardGameProductMapper;
import com.itheima.prize.commons.db.mapper.CardGameRulesMapper;
import com.itheima.prize.commons.db.mapper.CardProductMapper;
import com.itheima.prize.commons.db.service.CardGameService;
import com.itheima.prize.commons.db.mapper.CardGameMapper;
import com.itheima.prize.commons.utils.ApiResult;
import com.itheima.prize.commons.utils.RedisUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author shawn
 * @description 针对表【card_game】的数据库操作Service实现
 * @createDate 2023-12-26 11:58:48
 */
@Service
public class CardGameServiceImpl extends ServiceImpl<CardGameMapper, CardGame>
        implements CardGameService {

    @Resource
    private CardProductMapper cardProductMapper;

    @Resource
    private CardGameProductMapper cardGameProductMapper;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private CardGameRulesMapper cardGameRulesMapper;

    /**
     * 根据活动状态获取活动信息
     *
     * @return
     */
    @Override
    public ApiResult<Page<CardGame>> getGameListByStatus(int status, int currPage, int limit) {
        Page<CardGame> gamePage = new Page<>(currPage, limit);

        LambdaQueryWrapper<CardGame> queryWrapper = new LambdaQueryWrapper<>();
        if (status != SystemConstants.MINUS_ONE) {
            queryWrapper.eq(CardGame::getStatus, status);
        }

        Page<CardGame> cardGamePage = this.page(gamePage, queryWrapper);

        return ApiResult.success(cardGamePage);
    }

    /**
     * 根据活动id查询活动信息
     */
    @Override
    public CardGame getGameInfoById(int gameId) {
        return this.getById(gameId);
    }

    /**
     * 缓存活动相关信息
     */
    @Override
    public void cacheGameInfo(int gameId) {
        // 活动基本信息
        CardGame cardGame = this.getGameInfoById(gameId);
        if (cardGame == null) {
            return;
        }

        redisUtil.set(RedisKeys.INFO + gameId, cardGame, -1);

        // 活动策略信息
        LambdaQueryWrapper<CardGameRules> rulesQueryWrapper = new LambdaQueryWrapper<>();
        rulesQueryWrapper.eq(CardGameRules::getGameid, gameId);
        List<CardGameRules> cardGameRules = cardGameRulesMapper.selectList(rulesQueryWrapper);
        cardGameRules.forEach(rules -> {

            redisUtil.hset(RedisKeys.MAXGOAL + gameId, rules.getUserlevel().toString(), rules.getGoalTimes());
            redisUtil.hset(RedisKeys.MAXENTER + gameId, rules.getUserlevel().toString(), rules.getEnterTimes());
        });

        // 奖品信息
        LambdaQueryWrapper<CardGameProduct> cardGameProductWrapper = new LambdaQueryWrapper<>();
        cardGameProductWrapper.eq(CardGameProduct::getGameid, gameId);
        List<CardGameProduct> cardGameProducts = cardGameProductMapper.selectList(cardGameProductWrapper);
        List<Integer> ProductIdList = cardGameProducts.stream()
                .filter(info -> info.getGameid() == gameId)
                .map(CardGameProduct::getProductid).collect(Collectors.toList());

        // 根据活动id列表获取到该活动的全部奖品信息
        List<CardProduct> cardProductList = cardProductMapper.getProductInfoByIds(ProductIdList);

        // 抽奖令牌

    }
}




